﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataAccess;
namespace QUANLYKHACHSAN_N4
{
    public partial class frmThongKe : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public frmThongKe()
        {
            InitializeComponent();
        }
        void HienThiBaoCaoChi()
        {
            if (!theoquy)
                dgrChi.DataSource = PhieuChi_Info.TongKeTheoThang(int.Parse(cbbThang.Text), int.Parse(txtNam.Text)).Tables[0];
            else
                dgrChi.DataSource = PhieuChi_Info.TongKeTheoQuy(int.Parse(cbbQuy.Text), int.Parse(txtNam.Text)).Tables[0];
            TongTienThu();
        }
        void HienThiDoanhThu()
        {
            dgrDoanhThu.DataSource = DoanhThu_Info.ThongKeTheoThang(int.Parse(cbbThang.Text), int.Parse(txtNam.Text)).Tables[0];
        }
        void HienThiBaoCaoThu()
        {
            if (!theoquy)

                dgrThu.DataSource = PhieuThu_Info.ThongKeTheoThang(int.Parse(cbbThang.Text), int.Parse(txtNam.Text)).Tables[0];
            else
                dgrThu.DataSource = PhieuThu_Info.ThongKeTheoQuy(int.Parse(cbbQuy.Text), int.Parse(txtNam.Text)).Tables[0];

        }

        private decimal TongTienThu()
        {
            decimal tongthu = 0;
            foreach (DataGridViewRow r in dgrThu.Rows)
                tongthu += decimal.Parse(r.Cells["SOTIEN"].Value.ToString());
            return tongthu;
        }
        private decimal TongTienChi()
        {
            decimal tongchi = 0;
            foreach (DataGridViewRow r in dgrChi.Rows)
                tongchi += decimal.Parse(r.Cells["SOTIENC"].Value.ToString());
            return tongchi;
        }
        private string TinhTrang(decimal thu, decimal chi)
        {
            string tt = "";
            if (thu > chi)
                return "Lãi";
            if (thu > chi)
                return "Lỗ";
            return "Hòa";

        }

        DataTable DoanhThu()
        {
            DataTable tb = new DataTable();            
            tb.Columns.Add("tongthu");
            tb.Columns.Add("tongchi");
            tb.Columns.Add("tienloi");
            tb.Columns.Add("tinhtrang");
            decimal tthu=TongTienThu();
            decimal tchi = TongTienChi();
            tb.Rows.Add(tthu, tchi, tthu - tchi, TinhTrang(tthu, tchi));
            return tb;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {

                HienThiBaoCaoThu();
                HienThiBaoCaoChi();
                dgrDoanhThu.DataSource = DoanhThu();
            }
            catch (Exception)
            {
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        bool theoquy = false;
        private void cbbQuy_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            if (cb == cbbQuy)
                theoquy = true;
            else
                theoquy = false;

        }

    }
}
